package cn.edu.xust.service.impl;

import cn.edu.xust.configuration.exception.Default200Exception;
import cn.edu.xust.configuration.exception.ErrorConstant;
import cn.edu.xust.dao.HotelDao;
import cn.edu.xust.entity.HotelEntity;
import cn.edu.xust.service.HotelService;
import cn.edu.xust.util.PageUtils;
import cn.edu.xust.util.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("hotelService")
public class HotelServiceImpl extends ServiceImpl<HotelDao, HotelEntity> implements HotelService {


    @Override
    public PageUtils queryPage(Map<String, Object> params, List<Integer> hotelIdList) {
        //这里得单独写
        //JSONObject params2 = (JSONObject) JSONObject.parse(params.get("params").toString());
        String key = (String) params.get("key");
        String search = (String) params.get("search");
        //模糊查询
        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(hotelIdList)) {
            wrapper.and(item -> {
                for (Integer integer : hotelIdList) {
                    item.or().eq("hotelId", integer);
                }
            });
        }

        if (!StringUtils.isEmpty(search)) {
            if (!StringUtils.isEmpty(key)) {
                wrapper.and(item -> {
                    item.eq(search, key)
                            .or().like(search, key)
                            .or().likeLeft(search, key)
                            .or().likeRight(search, key);
                });
            }
            ;
        } else {
            if (!StringUtils.isEmpty(key)) {
                wrapper.and(item -> {
                    item.eq("hotelId", key)
                            .or().like("hotelCname", key)
                            .or().like("hotelName", key)
                            .or().like("hotelStar", key)
                            .or().like("hotelAddress", key)
                            .or().like("hotelGroup", key)
                            .or().like("floor", key)
                            .or().like("acId", key)
                            .or().like("rcuId", key)
                            .or().like("tvId", key)
                            .or().like("tvMode", key)
                            .or().like("acMode", key);
                });
            }
        }
        IPage<HotelEntity> page = this.page(
                new Query<HotelEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }


    @Override
    public HotelEntity findByHotelId(Integer hotelId) {
        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("hotelId", hotelId);
        return baseMapper.selectOne(wrapper);
    }


    @Override
    public List<HotelEntity> getAllHotel() {
        return baseMapper.selectList(null);
    }


    @Override
    public List<HotelEntity> getByHotelGroupList(Integer item) {
        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("hotelGroup", item);
        return baseMapper.selectList(wrapper);
    }


    @Override
    public HotelEntity getByPmsId(String pmshotelId) {
        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pmshotelId", pmshotelId);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public List<HotelEntity> getByGroupId(String groupid) {
        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("hotelGroup", groupid);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public HotelEntity getByHotelid(String hotelid) {
        return baseMapper.selectOne(new QueryWrapper<HotelEntity>().eq("hotelId", hotelid));
    }

    @Override
    public List<HotelEntity> getByHotelidList(String[] hotellists) {
        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        wrapper.in("hotelId", hotellists);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<HotelEntity> getByGroupIdList(String[] hotelGroup) {

        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        wrapper.in("hotelGroup", hotelGroup);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<HotelEntity> getByProvinceList(String[] provinceid) {

        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        wrapper.in("provinceid", provinceid);
        return baseMapper.selectList(wrapper);


    }

    @Override
    public List<HotelEntity> getByCityList(String[] cityid) {
        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        wrapper.in("cityid", cityid);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public boolean updateEnv(String hotelid, String env_data) {
        return baseMapper.updateEnv(hotelid, env_data);
    }

    @Override
    public boolean updateAddress(String hotelid, String address) {
        return baseMapper.updateAddress(hotelid, address);

    }

    @Override
    public String getBigId() {
        return baseMapper.getBigId();
    }

    @Override
    public void saveWithValid(HotelEntity hotelEntity) throws Default200Exception {

        //检查酒店号是否存在
        HotelEntity byHotelid = this.getByHotelid(String.valueOf(hotelEntity.getHotelId()));
        if (byHotelid != null) {
            throw new Default200Exception(ErrorConstant.hotel_already_exist);
        }
        baseMapper.insert(hotelEntity);
    }

    @Override
    public List<HotelEntity> getByHotelidList(List<Integer> hotelids) {
        if (hotelids.size() == 0) {
            return new ArrayList<>();
        }


        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        wrapper.in("hotelId", hotelids);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<HotelEntity> getByPC(String pid, String cid) {
        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        wrapper.in("cityid", cid);
        wrapper.in("provinceid", pid);
        return baseMapper.selectList(wrapper);
    }



    @Cacheable(value ="appid",key="#appId" )
    @Override
    public HotelEntity getByAppid(String appId) {

        QueryWrapper<HotelEntity> wrapper = new QueryWrapper<>();
        wrapper.in("appid", appId);
        return baseMapper.selectOne(wrapper);

    }


}
